package com.nj.liandi.gulimall.seckill.controller;


import com.nj.liandi.common.utils.R;
import com.nj.liandi.gulimall.seckill.service.SeckillService;
import com.nj.liandi.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * description: 查出当前参与的秒杀商品
 * date: 2021/11/10 14:15
 * author: maizj
 * version: 1.0
 */
@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @ResponseBody
    @GetMapping("/currentSeckilllSkus")
    public R getCurrentSeckillSkus(){
        List<SeckillSkuRedisTo> skuRedisToList =seckillService.getCurrentSeckillSkus();

        return R.ok().setData(skuRedisToList);
    }

    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R getSkuSeckillInof(@PathVariable("skuId") Long skuId){
        SeckillSkuRedisTo to =seckillService.getSkuSeckillInof(skuId);
        return R.ok().setData(to);
    }


    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num, Model model){
        String orderSn =seckillService.kill(killId,key,num);
        model.addAttribute("orderSn",orderSn);
        return "success";
    }
}
